package com.thssh.ui.lsn16;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.thssh.ui.R;
import com.thssh.ui.base.BaseActivity;
import com.thssh.ui.lsn16.activity.AddCartActivity;

public class Lsn16Activity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lsn16);
    }

    private void xmlAnimator(View view){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_fade_in);
        view.startAnimation(animation);
    }

    private void startObjectAnimator(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0f, 200f, 0f);
        animator.setDuration(2000);
        animator.start();
    }

    /**
     * 组合动画1
     * @param view
     */
    private void comboAnimByListener(final View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0f, 200f);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                view.setScaleX(value / 200f);
                view.setScaleY(value / 200f);
            }
        });
        animator.start();
    }

    /**
     * 组合动画2
     * @param view
     */
    private void comboAnimByListener2(final View view) {
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 200f);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                view.setTranslationX(value);
                view.setScaleX(value / 200f);
                view.setScaleY(value / 200f);
            }
        });
        animator.start();
    }

    /**
     * 组合动画3
     * @param view
     */
    private void PropertyValuesHolder(View view){
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f, 1f);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f, 1f);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f, 1f);

        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2, holder3);
        animator.setDuration(500);
        animator.start();
    }

    /**
     * 组合动画4：动画集合
     * @param view
     */
    private void animatorSet(View view) {
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationX", 0f, 200f, 0f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f, 1f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.5f, 1f);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        // 同时执行
        set.playTogether(animator1, animator2, animator3);
        // 顺序执行
//        set.playSequentially(animator1, animator2, animator3);
        set.start();
    }

    public void xmlAnimClick(View view) {
        xmlAnimator(view);
    }

    public void objectAnimClick(View view) {
        startObjectAnimator(view);
    }

    public void comboAnimClick(View view) {
        // 方法一：动画监听，同步执行其他动作
//        comboAnimByListener(view);
        // 方法二：
//        comboAnimByListener2(view);
        // 方法三：
//        PropertyValuesHolder(view);
        // 方法四：动画集合
        animatorSet(view);
    }

    public void demoClick(View view) {
        startActivity(AddCartActivity.class);
    }
}
